package com.lantosec.hmrp.modules.supplier.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.core.config.InitDataListener;
import com.lantosec.hmrp.core.shiro.TokenUtil;
import com.lantosec.hmrp.core.util.UUIDUtils;
import com.lantosec.hmrp.modules.supplier.entity.SupplierAccessory;
import com.lantosec.hmrp.modules.supplier.mapper.SupplierAccessoryMapper;
import com.lantosec.hmrp.modules.supplier.service.ISupplierAccessoryService;
import com.lantosec.hmrp.modules.supplier.vo.SupplierAccessoryVO;
import com.lantosec.hmrp.modules.system.service.ISysDictionariesService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 供应商附件信息 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-02-20
 */
@Service
@Primary
@Log4j2
public class SupplierAccessoryServiceImpl extends ServiceImpl<SupplierAccessoryMapper, SupplierAccessory> implements ISupplierAccessoryService {

    @Autowired
    private ISysDictionariesService iSysDictionariesService;

    /**
     * 查询附件信息
     * @param accessoryType
     * @return
     */
    @Override
    public List<SupplierAccessoryVO> querySupplierAccessory(String supplierCode,String accessoryType) {
        List<SupplierAccessoryVO> list = baseMapper.querySupplierAccessory(supplierCode,accessoryType);
        list.forEach(parm->{
            //查询字典表中附件类型名称
            String accessoryType1 = parm.getAccessoryType();
            if(StringUtils.isNotEmpty(accessoryType1)){
                String accessoryTypeName = iSysDictionariesService.getById(accessoryType1).getDictionariesShowName();
                parm.setAccessoryTypeName(accessoryTypeName);
            }
            parm.setSavePath(InitDataListener.uploadsUrl+parm.getSavePath()+parm.getFileName());
        });
        return list;
    }

    /**
     * 查询附件信息以及附件存储路径
     * @param supplierCode
     * @return
     */
    @Override
    public List<SupplierAccessoryVO> querySupplierAccessoryPath(String supplierCode) {
        List<SupplierAccessoryVO> list = baseMapper.querySupplierAccessoryPath(supplierCode);
        list.forEach(parm->{
            //查询字典表中附件类型名称
            String accessoryType = parm.getAccessoryType();
            if(StringUtils.isNotEmpty(accessoryType)){
                String accessoryTypeName = iSysDictionariesService.getById(accessoryType).getDictionariesShowName();
                parm.setAccessoryTypeName(accessoryTypeName);
            }
            parm.setSavePath(InitDataListener.uploadsUrl+parm.getSavePath()+parm.getFileName());
        });
        return list;
    }

    @Override
    public void insertSupplierAccessory(List<SupplierAccessoryVO> voList) {
        voList.forEach(parm->{
            parm.setAccessoryId(UUIDUtils.get());
            //获得redis中的用户名和id
            String userId = TokenUtil.getUserId();
            String name = TokenUtil.getName();
            parm.setCrateUserId(userId);
            parm.setCreateUser(name);
            parm.setCreateTime(new Date());
            this.save(parm.convertToEntity());
        });
    }


}
